package middlewares

import (
	"github.com/gin-gonic/gin"
	"golang.org/x/exp/slices"
	"net/url"
	"tools-api/src/common/def"
)

func Ignore() gin.HandlerFunc {
	return func(c *gin.Context) {
		path := getBasePath(c.Request.RequestURI)
		if path == "/" || path == "/favicon.ico" {
			c.AbortWithStatus(200)
			return
		}
		list := def.Config.GetStringSlice("app.ignore") // 有风险项，直接拦截
		if len(list) > 0 {
			if slices.Contains(list, path) {
				c.AbortWithStatus(404)
				return
			}
		}
		c.Next()
	}
}

func getBasePath(rawUrl string) string {
	u, err := url.Parse(rawUrl)
	if err != nil {
		return rawUrl
	}
	return u.Path
}
